/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.sip;

import java.util.List;
import java.util.Vector;
import java.util.concurrent.ArrayBlockingQueue;

/**
 *
 * @author sriram
 */
public class sipMessageAcceptor extends Thread{
    // Set the number of maximum messages to be 100
    private static final int MAX_MESSAGES_QUEUED = 100;
    private static final int MAX_MESSAGES_PER_CYCLE = 10;
    private boolean msgDebug = true;
    private boolean debugEnabled = true;
    List<String> msg = new Vector(1);
    ArrayBlockingQueue msgQ = null;

    public sipMessageAcceptor() {
        msgQ = new ArrayBlockingQueue(MAX_MESSAGES_QUEUED);
    }

    @Override
    public void run() {
        while(true) {
            try {
                // Sleep for 200 milliseconds before checking
                // for new messages to process
                sleep(200);
                checkForNewMessages();
            } catch(InterruptedException e) {
                checkForNewMessages();
            }
        }
    }
    
    public void checkForNewMessages() {
        String temp = null;
        for(int i=0; i<MAX_MESSAGES_PER_CYCLE; i++) {
            if((temp = (String) msgQ.poll()) != null) {
                if(debugEnabled)
                    System.out.println("sipMessageAcceptor::checkForNewMessages-Total Queue size"+msgQ.size());
                processMessage(temp);
            } else {
                break;
            }
        }
    }
    
    public boolean processMessage (String strMsg) {
        if(msgDebug) {
            System.out.println("sipMessageAcceptor::processMessage-Incoming Message received");
            System.out.println(strMsg);
        }
        if(debugEnabled) {
            //System.out.println("sipMessageAcceptor::processMessage-"+strMsg);
            int index = strMsg.indexOf("\n");
            if(index >= 0)
                System.out.println("sipMessageAcceptor::processMessage-First Line: "+strMsg.substring(0, index));
            else
                System.out.println("sipMessageAcceptor::processMessage-Full: "+strMsg);
        }
        sipCallManager mgr = sipCallManager.getInstance();
        sipParser parser = null;
        sipMessage sipMsg = null;
        String[] str = strMsg.split("\r\n");
        msg.clear();
        for(int i=0; i<str.length; i++)
            msg.add(str[i]);

        parser = new sipParser(msg);
        if(!parser.parse()) {
            if(debugEnabled)
                System.out.println("sipMessageAcceptor::processMessage-Parsing ERROR!");
            return false;
        }
        if((sipMsg = parser.getMessage()) == null) {
            if(debugEnabled)
                System.out.println("sipMessageAcceptor::processMessage-Parsing not Completed!");
            return false;
        }
        
        if(sipMsg.isRequest) {
            switch(sipMsg.method) {
                case INVITE:
                    mgr.processInvite(sipMsg);
                    break;
                case ACK:
                    mgr.processAck(sipMsg);
                    break;
                case BYE:
                    mgr.processBye(sipMsg);
                    break;
                case MESSAGE:
                    // Adding support for SIP MESSAGE method for
                    // the speech manager to utilize it for speech control
                    mgr.processMessage(sipMsg);
                    break;
                default:
                    if(debugEnabled)
                        System.out.println("sipMessageAcceptor::processMessage-Invalid method Type: "+sipMsg.method);
                    break;
            }
        } else {
            if(debugEnabled)
                System.out.println("sipMessageAcceptor::processMessage-Received 200 OK");
            mgr.process200OK(sipMsg);
        }
        return true;
    }

    public boolean queue(String msg) {
        try {
            if(debugEnabled)
                System.out.println("sipMessageAcceptor::send-Message sucessfully queued");
            return msgQ.add(msg);
        } catch (Exception e) {
            if(debugEnabled)
                System.out.println("sipMessageAcceptor::send-Unable to queue the message: " + e.getMessage());
            return false;
        }

    }
}
